<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>新增页面</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>

<body>
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" id="username" lay-verify="required" autocomplete="off"
                            placeholder="请输用户名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">微信号</label>
                    <div class="layui-input-block">
                        <input type="text" name="wechat_number" id="wechat_number" lay-verify="" autocomplete="off"
                            placeholder="请输入微信号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">微信昵称</label>
                    <div class="layui-input-block">
                        <input type="text" name="wechat_nickname" id="wechat_nickname" lay-verify="" autocomplete="off"
                            placeholder="请输入微信昵称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" id="phone" lay-verify="required|veryifyPhone" autocomplete="off" placeholder="请输入手机号"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">年龄</label>
                    <div class="layui-input-block">
                        <input type="text" name="age" id="age" lay-verify="" autocomplete="off" placeholder="请输入年龄"
                            value="18" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">抽佣比例</label>
                    <div class="layui-input-block">
                        <input type="text" name="ratio" id="ratio" lay-verify="veryify_sort" autocomplete="off"
                            placeholder="请输入抽佣比例" class="layui-input" value="3">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="gender" value="0" title="男" lay-skin="tag" checked>
                        <input type="radio" name="gender" value="1" title="女" lay-skin="tag">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">岗位</label>
                    <div class="layui-input-block">
                        <input type="radio" name="position" value="0" title="外协" lay-skin="tag">
                        <input type="radio" name="position" value="1" title="员工" lay-skin="tag" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">在职状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="staff_status" value="0" title="离职" lay-skin="tag">
                        <input type="radio" name="staff_status" value="1" title="在职" lay-skin="tag" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序号</label>
                    <div class="layui-input-block">
                        <input type="text" name="sort_num" id="sort_num" lay-verify="required|veryify_sort"
                            autocomplete="off" value="0" placeholder="请输入排序号" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="base-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    <script src="../../../component/com/api.js"></script>
    <script src="../../../component/com/auth.js"></script>
    <script src="../../../component/layui/layui.js"></script>
    <script src="../../../component/pear/pear.js"></script>
    <script>
        layui.use(['form', 'jquery'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let API_BASE_URL = apiConfig.baseUrl;
            let token = window.localStorage.getItem("token") || window.sessionStorage.getItem("token")
            const userInfoStr = window.localStorage.getItem("user_info") || window.sessionStorage.getItem("user_info");
            const user_org_id = userInfoStr ? JSON.parse(userInfoStr).org_id : null;

            form.verify({
                veryify_sort: function (value) {
                    if (!/^\d+$/.test(value)) {
                        return "请输入正整数！"
                    }
                    if (parseInt(value) < 0) {
                        return "请输入正整数！"
                    }
                },
                // 手机号验证
                veryifyPhone: function (value) {
                    // 严格的手机号正则（11位，以13/14/15/16/17/18/19开头）
                    const reg = /^1[3-9]\d{9}$/;
                    if (value.length > 0 && !reg.test(value)) {
                        return "请输入11位有效手机号！";
                    }
                }
            })

            form.on('submit(base-save)', function (data) {
                let formData = {
                    ...data.field,  // 保留其他字段
                    org_id: user_org_id,
                    password: 123,  // 使用第一个密码输入框的值
                };
                $.ajax({
                    url: `${API_BASE_URL}/user/`,
                    // data: JSON.stringify(data.field),
                    data: JSON.stringify(formData), 
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    headers: {
                        "Authorization": token
                    },
                    success: function (result) {
                        console.log('result=====', result)
                        if (result.code == 200) {
                            layer.msg(result.message, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                                parent.layui.table.reload("base-table");
                            });
                        } else {
                            // 错误处理
                            let errorMessage = result.message;

                            // 尝试获取data中的第一个错误信息
                            if (result.data && typeof result.data === 'object') {
                                const firstKey = Object.keys(result.data)[0];
                                if (firstKey) {
                                    errorMessage = result.data[firstKey][0]; // 获取第一个字段的第一个错误
                                }
                            }

                            layer.msg(errorMessage, { icon: 2, time: 2000 });
                        }
                    },
                    error: function () {
                        console.log('AJAX 请求失败')
                        layer.msg("网络请求异常", { icon: 1, time: 1000 })
                    }
                })
                return false;
            });
        })
    </script>
    <script>
    </script>
</body>

</html>